import { login } from '@/api/user'
import { Message } from 'element-ui'
const state = {
  token: null,
  userInfo: {},
  routerName: null
}
const mutations = {
  setToken (state, token) {
    state.token = token
  },
  delToekn (state) {
    state.token = null
  },
  setUserInfo (state, userInfo) {
    state.userInfo = userInfo
  },
  removeUserInfo (state) {
    state.userInfo = {}
  },
  setrouterName (state, routerName) {
    state.routerName = routerName
  },
  delrouterName (state) {
    state.routerName = null
  }
}
const actions = {

  // async loginAction (context, data) {
  //   // const res = await loginApi(data)
  //   console.log(data)
  //   context.commit('setToken', '123456')
  // },
  async loginAction ({ commit }, data) {
    try {
      const res = await login(data)
      console.log(res.code)
      if (res.code === 200) {
        commit('setUserInfo', res.data)
      } else if (res.code === 222) {
        Message.error(res.msg)
      }
    } catch (err) {
      console.log(err)
    }
  },
  logout ({ commit }) {
    commit('delToekn')
    commit('removeUserInfo')
  },
  // 顶栏路由名
  updataRouterName (context, payload) {
    context.commit('setrouterName', payload)
  },
  removeRouterName ({ commit }) {
    commit('delrouterName')
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
